package org.misalen.web.server.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * 
 * map工具类
 * 
 * @author guochao
 *
 */
public class MapUtil {

	/**
	 * 类转map
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> convertObjToMap(Object obj) throws Exception {
		Map<String, Object> reMap = new HashMap<String, Object>();
		if (obj == null)
			return null;
		String[] filter;
		JsonIgnoreProperties classAnnotation = obj.getClass().getAnnotation(JsonIgnoreProperties.class);
		if (classAnnotation != null) {
			filter = classAnnotation.value();
		} else {
			filter = new String[] {};
		}
		Field[] fields = obj.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field f = obj.getClass().getDeclaredField(fields[i].getName());
			f.setAccessible(true);
			Object o = f.get(obj);
			String name = fields[i].getName();
			reMap.put(name, o);
			for (String string : filter) {
				if (string.equals(name)) {
					reMap.remove(name);
					break;
				}
			}
		}
		return reMap;
	}
}
